﻿<?php
    include_once("../action/checkAuthenticationAction.php");
    include_once("../service/branchService.php");
	include_once("../util/constant.php");
    include_once("../util/message.php");
    
    include_once("../util/authUtil.php");
    $perm_array = $_SESSION["permissions"];
?>
<!DOCTYPE html>
<head>
	<title>Dashboard</title>
	<?php include("import-css.php");?>	
    <style>
        
    </style>
</head>
<body>
	<?php include("header.php");?>
    
    <div class="container-fluid">
		<div class="row-fluid">
            <!--Menu-->
            <?php include("menu.php");?>
            
            <?php
                $action = isset($_GET["action"]) ? $_GET["action"] : 8;
                if($action == EDIT_ACTION || $action == INSERT_SUCCESSFULLY || $action == UPDATE_SUCCESSFULLY){
                    $bra_id = $_GET["id"];
                    $edit_branch = getBranch($bra_id);
                }
            ?>
            <?php
				$class = null;
				$message = null;
                if(isset($_GET["action"])){
				$action = $_GET["action"];
                        if($action == INSERT_FAILED){
							$message = $messages["createBranchFailed"];
							$class = FAILED_MESSAGE_CLASS;
						}else if($action == UPDATE_FAILED){
							$message = $messages["updateBranchFailed"];
							$class = FAILED_MESSAGE_CLASS;						  
						}else if($action == INSERT_SUCCESSFULLY){
						    $message = $messages["createBranchSuccessfully"];
							$class = SUCCESSFULL_MESSAGE_CLASS;
						}else if($action == UPDATE_SUCCESSFULLY){
                            $message = $messages["updateBranchSuccessfully"];
							$class = SUCCESSFULL_MESSAGE_CLASS;
						}
                		if(isset($class)){
			?>
						<div id="msg" class="center alert <?php echo($class);?>"><?php echo($message);?></div>
			<?php		
					   }
				}
			?> 
            <!--Content-->
            <div class="box span10 offset2">
                <div class="box-content">
                    <?php
                    if(!authUtil(array("edit branch","create branch","view branch"),$perm_array,false)){
                        echo "<script>window.location.href='../admin/index.php';</script>";
                    }
                    $require_perm = array("edit branch","create branch");
                    ?>
					<form class="form-horizontal" method="POST" action="<?php if(authUtil($require_perm,$perm_array,false)){echo ($action == NEW_ACTION || $action == INSERT_FAILED || $action == UPDATE_FAILED) ? "../action/newBranchAction.php" : "../action/editBranchAction.php?id=".$edit_branch["BRA_ID"];} ?>" id="new_branch_form">
                        <div class="control-group">
                            <label for="branchTitle" class="control-label">Tên chi nhánh</label>
                            <div class="controls">
                                <input required type="text" value="<?php echo ($action == EDIT_ACTION || $action == INSERT_SUCCESSFULLY || $action == UPDATE_SUCCESSFULLY ?  $edit_branch["BRA_NAME"] : ""); ?>" id="branchTitle" name="branchTitle" class="input-xlarge focused">
                            </div>
                        </div>
						<div class="control-group">
                            <label class="control-label" for="order">Thứ tự (sắp xếp tăng dần)</label>
                            <div class="controls">
                                 <select id="order" name="order" onchange=''>
                                    <?php
                                        for($i=1;$i<=100;$i++){
									?>
										<option <?php echo ($action==EDIT_ACTION && $edit_branch["BRA_ORDER"]==$i) ? "selected" : "" ?> value='<?php echo $i; ?>'><?php echo $i; ?></option>
									<?php
										}
                                    ?>
                                 </select>
                            </div>
                        </div>
						<div class="control-group">
							<label class="control-label" for="textarea2">Thông tin chi nhánh</label>
							<div class="controls">
								<textarea class="ckeditor" id="textarea2" rows="3" name="address">
									<?php echo ($action == EDIT_ACTION || $action == INSERT_SUCCESSFULLY || $action == UPDATE_SUCCESSFULLY ?  $edit_branch["BRA_ADDRESS"] : ""); ?>
								</textarea>
							</div>
						</div>
						
                        <?php 
                            if(authUtil($require_perm,$perm_array,false)){
                        ?>
                        <center>
                        <button type="submit" class="btn btn-primary">
							<?php echo (($action == NEW_ACTION || $action == INSERT_FAILED || $action == UPDATE_FAILED) ? "Tạo mới" : "Cập nhật"); ?>
						</button>
                        </center>
                        <?php
                            }
                        ?>
					</form>
				</div>                
            </div><!--/span-->
        </div>
    </div>

    <?php include("footer.php");?>
		
	</div><!--/.fluid-container-->

	<?php include("import-js.php");?>	
</body>
</html>